"""
改变当前的屏幕方向,portrait为竖屏,landscape为横屏
"""
from adbutils import adb


def get_screen_orientation():
    """
    Get the screen orientation of the device.
    :return: 'portrait' if the screen orientation is portrait, 'landscape' if it's landscape.
    """
    # Connect to the device
    device = adb.device()

    # curSize = device.shell("wm size")
    # print(curSize)

    # Execute adb shell command to get the screen orientation
    output = device.shell(['dumpsys', 'input', '|\grep', 'SurfaceOrientation'])

    # Extract the orientation from the output
    orientation = 'portrait' if 'SurfaceOrientation: 0' in output else 'landscape'

    return orientation


def set_screen_orientation(orientation):
    """
    Set the screen orientation of the device.
    :param orientation: The orientation to set, either 'portrait' or 'landscape'.
    """
    # Connect to the device
    device = adb.device()

    # Determine the orientation value based on the desired orientation
    orientation_value = '0' if orientation == 'landscape' else '1'

    # Execute adb shell command to set the screen orientation
    device.shell(['settings', 'put', 'system', 'user_rotation', orientation_value])

    print("Screen orientation set to", orientation)


if __name__ == "__main__":
    # 获得当前的屏幕方向
    current_orientation = get_screen_orientation()

    # 更改当前的屏幕方向
    # set_screen_orientation('portrait')
    set_screen_orientation('landscape')
